package com.sam.mooc;

import java.util.ArrayList;
import java.util.List;

/**
 * 题目 http://www.icourse163.org/learn/ZJU-1001542001?tid=1002311011#/learn/ojhw?id=1003276169
 * Created by samwang on 2017/11/26.
 */
public class City {
    public static void main(String[] args) {
        java.util.Scanner in = new java.util.Scanner(System.in);
        List<String> cityName = new ArrayList<String>();
        while (true) {
            String s = in.next();
            if (s.equals("###")) {
                break;
            }
            cityName.add(s);

        }
        int n = cityName.size();
        ArrayList<ArrayList<Integer>> distance = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> temp = new ArrayList<Integer>();
        distance.add(temp);
        while (true) {
            temp.add(in.nextInt());
            if (temp.size() == n) {
                if (distance.size() == n) {
                    break;
                }
                temp = new ArrayList<Integer>();
                distance.add(temp);
            }

        }
        int a = cityName.indexOf(in.next());
        int b = cityName.indexOf(in.next());
        int result;

        if (a == -1 || b == -1) {
            result = 0;
        }else if (a == b) {
            result = 0;
        } else {
            result = distance.get(a).get(b);
        }
        System.out.println(result);
    }
}
